package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements WechatService {
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    //获取openid
    @Override
    public String getOpenId(String code) {
        //请求微信接口
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        String result = HttpUtil.get(REQUEST_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);

        //如果errcode存在，则说明登录失败
        Object errcode = jsonObject.get("errcode");
        if(ObjectUtil.isNotEmpty(errcode)){
            throw new BaseException("微信登录失败");
        }

        //成功了，返回openid
        return jsonObject.getStr("openid");
    }

    /**
     * 获取手机号
     */
    @Override
    public String getPhone(String phoneCode) {
        //获取access_token
        Map<String, Object> accessMap = new HashMap<>();
        accessMap.put("appid", appid);
        accessMap.put("secret", secret);
        String accessResult = HttpUtil.get(TOKEN_URL, accessMap);
        JSONObject accessObject = JSONUtil.parseObj(accessResult);
        //如果errcode存在，则说明获取access_token失败
        Object accessErrorCode = accessObject.get("errcode");
        if(ObjectUtil.isNotEmpty(accessErrorCode)){
            throw new BaseException("获取access_token失败");
        }
        //获取access_token成功
        String accessToken = accessObject.getStr("access_token");

        //获取手机号码
        String url = PHONE_REQUEST_URL + accessToken;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", phoneCode);
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //如果errCode存在，则说明获取号码失败
        int errorCode = jsonObject.getInt("errcode");
        if(errorCode != 0){
            throw new BaseException("获取号码失败");
        }

        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

}
